package com.livery.app.view.frame;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.SwingConstants;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.TableModel;

import org.hibernate.Query;
import org.hibernate.Session;

import com.livery.app.controller.AbstractWorkspaceController;
import com.livery.app.main.HibernateUtil;
import com.livery.app.model.AbstractWorkspaceModel;
import com.livery.app.model.observators.CarObserver;
import com.livery.app.model.observators.TransactionObserver;
import com.livery.app.model.tables.CarTableModel;
import com.livery.data.Car;

public class CarFrame extends JFrame implements CarObserver, ActionListener{
	
	private AbstractWorkspaceController controller;
	
	private JTextField fieldMark;
	private JTextField fieldModel;
	private JTextField fieldYear;
	
	private JButton btnSearch;
	private JButton btnAddCar;
	private JButton btnEditCar;
	private JButton btnArchiveCar;
	
	private List<Car> listCar = new ArrayList<Car>();
	private JTable tableCar;
	private TableModel model;
	
	public CarFrame(AbstractWorkspaceController controller){
		super("Samochody");
		this.setMinimumSize(new Dimension(800,600));
		this.addWindowListener	(new WindowAdapter(){ 
			public void windowClosing(WindowEvent e){ 
				setVisible(false);				
			}	
		});	

		Dimension dialogSize = getSize();		
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();  
		if(dialogSize.height > screenSize.height) 
			dialogSize.height = screenSize.height;
		if(dialogSize.width > screenSize.width)
			dialogSize.height = screenSize.width;
		setLocation((screenSize.width-dialogSize.width)/2,   
						(screenSize.height-dialogSize.height)/2);
		this.controller = controller;
		createView();
	}
	private void createView(){
		this.setLayout(new BorderLayout());
		this.add(createNorthPanel(), BorderLayout.NORTH);
		this.add(createCenterPanel(), BorderLayout.CENTER);
		this.add(createSouthPanel(), BorderLayout.SOUTH);
	}
	private JButton createButton(String text, String toolTip){
		JButton button = new JButton(text);
		button.setToolTipText(toolTip);
		button.addActionListener(this);
		button.setEnabled(true);
		return button;
	}
	
	private JPanel createNorthPanel(){
		JPanel panel = new JPanel(new FlowLayout());
		JLabel labelMark = new JLabel("Marka:", SwingConstants.RIGHT);
		JLabel labelModel = new JLabel("Model:", SwingConstants.RIGHT);
		JLabel labelYear = new JLabel("Rok produkcji:", SwingConstants.RIGHT);
		fieldMark = new JTextField(10);
		fieldModel = new JTextField(10);
		fieldYear = new JTextField(10);
		btnSearch = createButton("Szukaj", "Wyszukaj samochody");
		panel.add(labelMark);
		panel.add(fieldMark);
		panel.add(labelModel);
		panel.add(fieldModel);
		panel.add(labelYear);
		panel.add(fieldYear);
		panel.add(btnSearch);
		return panel;
	}
	private JPanel createSouthPanel(){
		JPanel panel = new JPanel(new FlowLayout());
		btnAddCar = createButton("Dodaj", "Dodaj nowy samochód");
		btnEditCar = createButton("Edytuj", "Edytuj aktualnie zaznaczony samochód");
		btnArchiveCar = createButton("Archiwizuj", "Przenieś aktualnie zaznaczony samochód do archiwum");
		panel.add(btnAddCar);
		panel.add(btnEditCar);
		panel.add(btnArchiveCar);
		return panel;
	}
	private JPanel createCenterPanel(){
		JPanel panel = new JPanel(new BorderLayout());
		
		model = new CarTableModel(listCar);
		tableCar = new JTable(model);
		tableCar.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
	    ListSelectionModel selectionModel = tableCar.getSelectionModel();
		selectionModel.addListSelectionListener(new ListSelectionListener() {
			
			@Override
			public void valueChanged(ListSelectionEvent arg0) {
				// TODO Auto-generated method stub
				setEnabledButtonEditArchive();
			}
		});
		JScrollPane scrollPane = new JScrollPane(tableCar);
		panel.add(scrollPane);
		return panel;
	}
	public void setDisabledButtonEditArchive(){
		setClearSelection();
		btnEditCar.setEnabled(false);
		btnArchiveCar.setEnabled(false);
	}
	private void setEnabledButtonEditArchive(){
		btnEditCar.setEnabled(true);
		btnArchiveCar.setEnabled(true);
	}
	private void setClearSelection(){
		tableCar.clearSelection();
	}
	@Override
	public void updateCar(List<Car> list) {
		listCar = list;
		((CarTableModel)model).setCarList(listCar);
		tableCar.revalidate();
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource() == btnSearch){
			Date date = null;
			String make = fieldMark.getText();
			String model = fieldModel.getText();
			String year = fieldYear.getText();
			DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
			try {
				date = format.parse(year);
			} catch (ParseException e1) {

			}
			controller.searchCar(make, model, date);
			
		}
		if(e.getSource() == btnAddCar){
			
		}
		if(e.getSource() == btnEditCar){
			
		}
		if(e.getSource() == btnArchiveCar){
			
		}
	}
}
